package com.fzl.springblogdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzl.springblogdemo.common.constant.Constants;
import com.fzl.springblogdemo.common.exception.BlogException;
import com.fzl.springblogdemo.mapper.BlogInfoMapper;
import com.fzl.springblogdemo.pojo.dataobject.BlogInfo;
import com.fzl.springblogdemo.pojo.request.AddBlogRequest;
import com.fzl.springblogdemo.pojo.request.UpdateBlogRequest;
import com.fzl.springblogdemo.pojo.response.BlogInfoResponse;
import com.fzl.springblogdemo.util.BeanTransUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BlogServiceImpl implements BlogService {


    @Resource
    private BlogInfoMapper blogInfoMapper;
    @Override
    public List<BlogInfoResponse> getList() {
        QueryWrapper<BlogInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, Constants.BLOG_NORMAL)
                .orderByDesc(BlogInfo::getCreateTime);
        List<BlogInfo> blogInfos=blogInfoMapper.selectList(queryWrapper);

        List<BlogInfoResponse> blogInfoResponses= blogInfos.stream()
                .map(blogInfo-> BeanTransUtils.trans(blogInfo))
                .collect(Collectors.toList());

        return blogInfoResponses;
    }

    @Override
    public BlogInfoResponse getBolgDetail(Integer blogId) {
        return BeanTransUtils.trans(getBolgInfo(blogId));
    }

    public BlogInfo getBolgInfo(Integer blogId) {
        QueryWrapper<BlogInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag,Constants.BLOG_NORMAL)
                .eq(BlogInfo::getId,blogId);
        return blogInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public Boolean addBlog(AddBlogRequest addBlogRequest) {
        BlogInfo blogInfo=new BlogInfo();
        BeanUtils.copyProperties(addBlogRequest,blogInfo);
        try{
            Integer result=blogInfoMapper.insert(blogInfo);
            if(result==1){
                return true;
            }
            return false;
        }catch (Exception e){
            log.error("博客插入失败："+e);
            throw new BlogException("博客插入失败，内部错误，请联系管理员");
        }

    }

    @Override
    public Boolean updateBlog(UpdateBlogRequest updateBlogRequest) {
        BlogInfo blogInfo = BeanTransUtils.trans(updateBlogRequest);
        try{
            Integer result = blogInfoMapper.updateById(blogInfo);
            return result == 1;
        }catch (Exception e){
            log.error("博客更新失败："+e);
            throw new BlogException("博客插入失败，内部错误，请联系管理员");
        }
    }

    @Override
    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(Constants.DEFAULT_FlAG);
        try{
            Integer result = blogInfoMapper.updateById(blogInfo);
            return result == 1;
        }catch (Exception e){
            log.error("博客删除失败："+e);
            throw new BlogException("博客插入失败，内部错误，请联系管理员");
        }
    }
}
